package com.oocl.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import com.oocl.dao.MerchantApplyRecordDao;
import com.oocl.dao.MerchantDao;
import com.oocl.dao.impl.MerchantApplyRecordDaoImpl;
import com.oocl.dao.impl.MerchantDaoImpl;
import com.oocl.pojo.Merchant;
import com.oocl.pojo.MerchantApplyRecord;
import com.oocl.service.MerchantManager;

public class MerchantManagerImpl implements MerchantManager{
	private MerchantDao merchantDao;
	private MerchantApplyRecordDao applyRecordDao;
	
	public MerchantManagerImpl(){
		merchantDao = new MerchantDaoImpl();
		applyRecordDao = new MerchantApplyRecordDaoImpl();
	}

	@Override
	public Merchant findMerchantById(String id) {
		Merchant merchant = merchantDao.findMerchantById(id);
		if(merchant!=null){
			return merchant;
		}else{
			return null;
		}
	}

	@Override
	public int getStatusById(String id) {
		int status = merchantDao.getStatusById(id);
		return status;
	}

	@Override
	public List<Merchant> findMerchantByStatus(Integer status) {
		List<Merchant> list = new ArrayList<Merchant>();
		list = merchantDao.findMerchantByStatus(status);
		return list;
	}

	@Override
	public int add(Merchant t) {
		int m = merchantDao.add(t);
		MerchantApplyRecord record = this.toApplyRecord(t);
		int n = applyRecordDao.add(record);
		return m+n;
	}

	@Override
	public int update(Merchant t) {
		int m = merchantDao.update(t);
		MerchantApplyRecord record = this.toApplyRecord(t);
		int n = applyRecordDao.add(record);
		return m+n;
	}

	@Override
	public int delete(String id) {
		int m=merchantDao.delete(id);
		return m;
	}

	@Override
	public List<Merchant> findAll() {
		List<Merchant> list = new ArrayList<Merchant>();
		list = merchantDao.findAll();
		return list;
	}

	@Override
	public List<Merchant> findMerchantByIsban(Boolean isBan) {
		List<Merchant> list = new ArrayList<Merchant>();
		list = merchantDao.findMerchantByBan(isBan);
		return list;
	}

	@Override
	public int updateStatus(String id, Integer status) {
		int m=merchantDao.updateStatus(id, status);
		Merchant merchant = merchantDao.findMerchantById(id);
		merchant.setStatus(status);
		MerchantApplyRecord record = this.toApplyRecord(merchant);
		int n = applyRecordDao.add(record);
		return m+n;
	}

	@Override
	public int updateIsBan(String id, Boolean isBan) {
		int m = merchantDao.updateBan(id, isBan);
		Merchant merchant = merchantDao.findMerchantById(id);
		merchant.setBan(isBan);
		MerchantApplyRecord record = this.toApplyRecord(merchant);
		int n = applyRecordDao.add(record);
		return m+n;
	}

	private MerchantApplyRecord toApplyRecord(Merchant t) {
		MerchantApplyRecord record = new MerchantApplyRecord();
		record.setId(null);
		record.setAddress(t.getAddress());
		record.setBan(t.isBan());
		record.setBcontent(t.getbContent());
		record.setHeading(t.getHeading());
		record.setIdcard(t.getIdcard());
		record.setmId(t.getId());
		record.setmName(t.getName());
		record.setStatus(t.getStatus());
		record.setId(UUID.randomUUID().toString());
		return record;
	}

	@Override
	public List<MerchantApplyRecord> findHistory(String id) {
		List<MerchantApplyRecord> list = new ArrayList<MerchantApplyRecord>();
		list = applyRecordDao.findByMerchantId(id);
		return list;
	}
}
